#pragma once

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <string>

class InetAddr
{
private:
    void Host(const struct sockaddr_in& addr)
    {
        // _ip = inet_ntoa(addr.sin_addr);
        _port = ntohs(addr.sin_port);
        char in_buf[32];
        ::inet_ntop(AF_INET, &addr.sin_addr, in_buf, sizeof(in_buf));
        _ip = in_buf;
    }
public:
    InetAddr(const struct sockaddr_in& addr) :_addr(addr)
    {
        Host(addr);
    }
    InetAddr() 
    {}
    uint16_t Port()
    {
        return _port;
    }

    const std::string& Ip()
    {
        return _ip;
    }
    bool operator == (const InetAddr& peer)
    {
        return (peer._ip == _ip && _port == peer._port);
    }
    struct sockaddr_in Addr()
    {
        return _addr;
    }
    std::string AddrStr()
    {
        return _ip + ":" + std::to_string(_port);
    }
private:
    uint16_t _port;
    std::string _ip;
    struct sockaddr_in _addr;
};